package barbre;

import java.io.*;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.*;


public class tokenisation {
	
	static List<HashMap<String,ArrayList<String>>> index = new ArrayList<HashMap<String,ArrayList<String>>>();

	public static String removeAccent(String source) {
		return Normalizer.normalize(source, Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
	}
	public static void createList() {
		
		String[] chaine ;
		
		try {
			InputStream ips=new FileInputStream("/home/cheikh/Bureau/fichier.txt"); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			addIndex("monindex1","page2");	addIndex("monindex2","page4"); 	addIndex("monindex3","page1"); 	addIndex("monindex4","page5");
			findIndex("monindex4","1");
			findIndex("monindex4","1");
			while ((ligne=br.readLine())!=null){
				ligne = removeAccent(ligne);
				ligne = ligne.replaceAll("[\\p{P} \\t\\n\\r]",":");
				ligne = ligne.replaceAll("::",":");
		
				//System.out.println(ligne);
				//chaine = ligne.split("[\\p{P} \\t\\n\\r]");
				chaine = ligne.split(":");
				for (int i = 0; i < chaine.length; i ++) {
						System.out.println(chaine[i]);
				}
				
				//System.out.println(ligne);
				//chaine+=ligne+"\n";
			}
			br.close(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String,ArrayList<String>> listIndex = new HashMap<String,ArrayList<String>>();
		List<HashMap<String,ArrayList<String>>> inde = new ArrayList<HashMap<String,ArrayList<String>>>();
		listIndex.put("index",new ArrayList<String>());
		inde.add(listIndex);
		// ajouter index dans la liste inde
		// ajouter un identifiant de fichier dans une liste pour un index donné
		//
		
		List<List<String>> index = new ArrayList<List<String>>();
		index.add(new ArrayList<String>());
		index.get(0).add("Hello");
		String result = index.get(0).get(0);
		index.add(new ArrayList<String>());
		index.get(1).add("Goodbye");
		for (List<String> l : index) {
			System.out.println("Start list");
			for (String s : l) {
				System.out.println("Entry:  " + s);
			}
		}
	}
	
	public static void addIndex(String oneindex, String id) {
		HashMap<String,ArrayList<String>> listIndex = new HashMap<String,ArrayList<String>>();
		listIndex.put(oneindex,new ArrayList<String>());
		listIndex.get(oneindex).add(id);
		index.add(listIndex);
		
		
	}
	
	public static boolean findIndex(String indexSearch, String id) {
		Iterator it  = index.iterator();
		int i=0;
		while (it.hasNext()) {
			HashMap<String,ArrayList<String>> myindex = (HashMap<String,ArrayList<String>>)it.next();
			if (myindex.containsKey(indexSearch)) {
				i=1;
				
				if (!myindex.get(indexSearch).contains(id)) {
					System.out.println("ajout dun nouveau id dans "+indexSearch);
					//ajouter page6:1
					myindex.get(indexSearch).add(id);
				} else {
					System.out.println("le mot  "+indexSearch+"estdeja localise dans cette page");
					// int position = myindex.get(indexSearch).indexOf(id);
					// set(position,inexSerach:nboccurences);
				}
				System.out.println("mon index xxxxxxx"+myindex);
			} /*else {
				System.out.println("dans le elseeeeeee");//addIndex(indexSearch);
			}*/
		}
		if (i == 0) {
			System.out.println("En vue d'ajouter l'index");
			addIndex(indexSearch,id);
		}
	
		return true;
	}
	public static boolean containsIdentifier(ArrayList<String> listId, String id) {
		Iterator it  = listId.iterator();
		while (it.hasNext()) {
			String identificator = (String) it.next();
			if  (identificator.split(":")[0].equals(id)) {
				return true;
			}
		}
		
		return false;
	}
	public static void main(String args[]) {
		createList();
	
	}
}
